//
//  LHPlaceholerTextView.swift
//  sinaWeibo
//
//  Created by LiHong on 15/11/4.
//  Copyright © 2015年 LiHong. All rights reserved.
//

import UIKit

class LHPlaceholerTextView: UITextView {
    //MARK: - 属性
    var placeholder: String? {
        didSet {
            placeholderLabel.text = placeholder
            placeholderLabel.font = font
            placeholderLabel.sizeToFit()
        }
    }
    
    //MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        prepareUI()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange", name: UITextViewTextDidChangeNotification, object: self)
    }
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    func textDidChange() {
        placeholderLabel.hidden = hasText()
    }
    
    //MARK: - 准备UI
    private func prepareUI() {
        addSubview(placeholderLabel)
        
        placeholderLabel.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: nil, offset: CGPoint(x: 5, y: 8))
    }
    
    
    //MARK - 懒加载
    private lazy var placeholderLabel: UILabel = {
        let label = UILabel(fontSize: 18, textColor: UIColor.lightGrayColor())
        
        return label
    }()
}
